package com.wyg.scg;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.plaf.synth.SynthSeparatorUI;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wyg.util.HibernateSessionFactory;

public class TestSCG {

	
	public static void main(String[] args) {
		//insertStudent();
		//deleteStudent();
		//findGrade("Lisi0");
		findStudent("语文");
	}
	
	//
	
	//查班级
	public static void findGrade(String name){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		String hql = "select s.grade from Student s where s.name=:name";
		Query query = session.createQuery(hql);
		query.setParameter("name", name);
		Grade g = (Grade) query.uniqueResult();
		System.out.println(g);
	}
	//查某门课学生
	public static void findStudent(String courseName){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		String hql = "select c.students from Course c where c.cname=:cname";
		Query query = session.createQuery(hql);
		query.setParameter("cname", courseName);
		
		List<Student> list = query.list();
		
		for(Student s :list){
			System.out.println(s);
		}
	}
	
	//删
	public static void deleteStudent(){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		Student stu = (Student) session.get(Student.class, 45);
		session.delete(stu);
		trans.commit();
	}
	//插
	public static void insertStudent(){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		Set<Course> courses = new HashSet<Course>();
		
		Grade g = new Grade();
		g.setGname("一年级");
		
		Course c = new Course();
		c.setCname("语文");
		Course cc = new Course();
		cc.setCname("数学");
		courses.add(c);
		courses.add(cc);
		
		for(int i=0;i<3;i++){
			Student stu = new Student();
			stu.setName("Lisi"+i);
			stu.setCourses(courses);
			stu.setGrade(g);
			session.save(stu);
		}
		trans.commit();
	}
}
